package t2h.gui;

import t2h.valueobjects.SongVO;

import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.plaf.UIManager;

public class SongDetail implements ActionListener{

	//Form f = new Form("Song Detail");
	SongVO songvo;
	private static final int BACK_COMMAND = 3;
    private static final int DEMOS_COMMAND = 9;
    private static final Command backCommand = new Command("Back", BACK_COMMAND);
    static final Command showDemosCommand = new Command("Demos", DEMOS_COMMAND);
    private Screen currentScreen;
    private static SongDetail instance;
    private static Form mainMenu;    
    static final Screen SCREEN = new SearchScreen();
    private Container demoPanel;
    private Container uiContent;
	
	public SongDetail(SongVO song){
		songvo = song;		
	}

	/*public Form execute(SongVO songvo) {
		// TODO Auto-generated method stub
		//Label lb = new Label(songvo.getId() + songvo.getAuthor());
		//f.addComponent(lb);
		//return f;
	}*/	
	

	public String getName() {
		// TODO Auto-generated method stub
		return "Song Detail";
	}
	void setMainForm() {
	    Form main;
	    SCREEN.run(backCommand, instance, instance.demoPanel);
	    if (Display.getInstance().isTablet()) {
	        main = new Form("");
	        main.setLayout(new BorderLayout());
	        demoPanel = new Container();
	        main.addComponent(BorderLayout.CENTER, demoPanel);
	        SCREEN.run(backCommand, SongDetail.this, demoPanel);
	        main.setScrollable(false);
	        Form uiContentParent = new Form("T2H Karaoke");
	        uiContentParent.setHideInPortrait(true);
	        uiContent = uiContentParent.getContentPane();
	        uiContentParent.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
	        main.addComponent(BorderLayout.WEST, uiContentParent);
	        main.addOrientationListener(new ActionListener() {
	
	            public void actionPerformed(ActionEvent evt) {
	                Form f = (Form) demoPanel.getComponentAt(0);
	                if (Display.getInstance().isPortrait()) {
	                    f.addCommand(showDemosCommand, 1);
	                }
	                else {
	                    f.removeCommand(showDemosCommand);
	                }
	            }
	        });
	    }
	    else {
	        main = new Form("T2H Karaoke");
	
	        uiContent = main.getContentPane();
	    }
	    if (mainMenu != null) {
	        main.setTransitionInAnimator(mainMenu.getTransitionInAnimator());
	        main.setTransitionOutAnimator(mainMenu.getTransitionOutAnimator());
	    }
	    mainMenu = main;
		
	    //mainMenu.addCommand(exitCommand);
	    //mainMenu.setBackCommand(exitCommand);
	    //mainMenu.addCommand(aboutCommand);    
	
	    mainMenu.addCommandListener(this);
	    if (Display.getInstance().getCurrent() != null) {
	        Display.getInstance().getCurrent().setTransitionOutAnimator(CommonTransitions.
	            createEmpty());
	    }
    }

	protected void executeDemo(Container f) {
		// TODO Auto-generated method stub
		mainMenu = (Form) f;
		Label lb = new Label(songvo.getId() +"-"+ songvo.getAuthor());
		f.addComponent(lb);
	}
	
	public final void run(final Command backCommand, ActionListener commandListener, final Container demoPanel) {
		final Form demoForm = new Form(getName());
        demoForm.addCommand(new Command("Help") {
            public void actionPerformed(ActionEvent evt) {
                Form helpForm = new Form("Help");
                //helpForm.setLayout(new BorderLayout());
                //TextArea helpText = new TextArea(getHelpImpl(), 5, 10);
                //helpText.setEditable(false);
                //helpForm.setScrollable(false);
                //helpForm.addComponent(BorderLayout.CENTER, helpText);
                Command c = new Command("Back") {
                    public void actionPerformed(ActionEvent evt) {
                        if(demoPanel != null) {
                            demoPanel.replace(demoPanel.getComponentAt(0), demoForm, UIManager.getInstance().getLookAndFeel().getDefaultFormTransitionOut().copy(true));
                        } else {
                            demoForm.showBack();
                        }
                    }
                };
                helpForm.addCommand(c);
                helpForm.setBackCommand(c);
                if(demoPanel != null) {
                    demoPanel.replace(demoPanel.getComponentAt(0), helpForm, UIManager.getInstance().getLookAndFeel().getDefaultFormTransitionOut());
                } else {
                    helpForm.show();
                }
            }
        });
        if(demoPanel == null) {
            demoForm.addCommand(backCommand);
            demoForm.setBackCommand(backCommand);
        }
        demoForm.addCommandListener(commandListener);
        executeDemo(demoForm);
        /*if(demoPanel != null) {
            if(Display.getInstance().isPortrait()) {
                demoForm.addCommand(ScreenMain.showDemosCommand, 1);
            }
            demoPanel.removeAll();
            demoPanel.addComponent(BorderLayout.CENTER, demoForm);
            demoPanel.revalidate();
        } else {
            demoForm.show();
        }*/
        demoForm.show();
	}

	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		currentScreen.cleanup();
        mainMenu.showBack();
	}

}
